package com.snj.visangedu.lib;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.TextView;

import com.snj.visangedu.news.News;
import com.snj.visangedu.news.NewsItem;
import com.snj.visangedu.news.NewsItem2;
import com.snj.visangedu.news.NewsItem3;

public class Global {
	
	public static boolean isshown = false;
	
	public static String pkgname = "com.snj.visangedu";
	
	public static String devicekey = "";
	public static String phone = "";
	public static String naverid = "";
	public static int level = 0;
	
	public static String baseurl = "http://snjksy.cafe24.com/snj/wef/";
	public static String vnpbaseurl = "http://viewnpop.com/snj/visangedu/";
	public static String godourl = "http://studyview.co.kr/studyviewapp/";
	public static String snjurl = "http://snjcommunication.cafe24.com/studyviewapp/";
	
	public static boolean ispaid = false;
	public static boolean ismonthly = false;
	
	public static String versionurl = godourl + "getversion.php";
	public static String memberurl = godourl + "member/getmemberinfo.php";
	public static String recordstudytimeurl = godourl + "studytime/addstudytime.php";
	public static String getstudytimeurl = godourl + "studytime/getstudytime.php";
	
	public static String getlastaskurl = godourl + "homestudy/getlastask.php";
	public static String getaskurl = godourl + "homestudy/getask.php";
	public static String getaskbysubjecturl = godourl + "homestudy/getaskbysubject.php";
	public static String addaskurl = godourl + "homestudy/add_ask.php";
	public static String addanswerurl = godourl + "homestudy/add_answer.php";
	public static String addaskimageurl = godourl + "homestudy/add_askimage.php";
	public static String addanswerimageurl = godourl + "homestudy/add_answerimage.php";
	public static String choiceaskurl = godourl + "homestudy/choiceask.php";
	public static String getaskitemcnturl = godourl + "homestudy/getaskitemcnt.php";
	 
	public static String clinic_askurl = godourl + "clinic/ask_email.php";
	
	public static String newsurl = vnpbaseurl + "getnews.php";
	public static String contenturl = vnpbaseurl + "getcontents.php";
	
	public static String newsurl2 = vnpbaseurl + "getnews2.php";
	public static String contenturl2 = vnpbaseurl + "getcontents2.php";
	
	public static String newsurl3 = vnpbaseurl + "getnews3.php";
	public static String contenturl3 = vnpbaseurl + "getcontents3.php";

	public static String listeningcontentsurl = godourl + "listening/getlisteningcontents.php";
	
	public static String wefmallurl = baseurl + "getwefmall.php";
	
	public static String eventurl = baseurl + "getevent.php";
	
	public static String couponurl = baseurl + "getcoupon.php";
	
	public static String zipcodeurl = baseurl + "getzipcode.php";
	
	
	
	public static ArrayList<NewsItem> mNews = new ArrayList<NewsItem>();
	public static ArrayList<NewsItem2> mNews2 = new ArrayList<NewsItem2>();
	public static ArrayList<NewsItem3> mNews3 = new ArrayList<NewsItem3>();
	
	public static ArrayList<News> ArrNews[] = new ArrayList[3];
	
	public static int loadingimgcount = 4;						
	public static int loadedimgcount = 0;						
	public static int totalloadedimgcount = 0;					
	
	public static int loadedimgcount2 = 0;						
	public static int totalloadedimgcount2 = 0;					
	
	
	public static SharedPreferences pref = null;
	
	public static StudyviewFragment PrevFragment = StudyviewFragment.EMPTY;
	public static StudyviewFragment CurrentFragment = StudyviewFragment.EMPTY;
	
	public static String ex_storage = Environment.getExternalStorageDirectory().getAbsolutePath();
	
	
	public static final int DIALOG_PROGRESS_WEBVIEW = 0;
	public static final int DIALOG_PROGRESS_MESSAGE = 1;
	public static final int DIALOG_ISP = 2; 
	public static final int DIALOG_CARDAPP = 3; 
	public static String DIALOG_CARDNM = ""; 
	
	
	public static boolean IsDownloading = false;
    
	
	public static boolean FileExist(String filename, String date)
	{
		File file = new File(ex_storage + filename);
		
		return file.exists();
	}
	
	public static void saveBitmaptoJpeg(Bitmap bitmap, String folder, String name)
	{
		
		
		String folder_name = "/"+folder+"/";
		String file_name = name;
		String string_path = ex_storage+folder_name;
		
		File file_path;
		
		Log.d("New_NewsAdapter", "Save module : " + string_path);
		try {
			file_path = new File(string_path);
			if(!file_path.isDirectory()) {
				file_path.mkdirs();
			}
			
			FileOutputStream out = new FileOutputStream(string_path+file_name);
			
			bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
			out.close();
		}
		catch(FileNotFoundException exception)
		{
			exception.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static int dpToPx(Context context, int dp) {
	    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
	    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));       
	    return px;
	}
	 
	public static int pxToDp(Context context, int px) {
	    DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
	    int dp = Math.round(px / (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
	    return dp;
	}
	
	public static final String TYPEFACE_1_NAME = "NanumGothic.ttf";
	public static final String TYPEFACE_2_NAME = "NanumBarunGothicUltraLight.ttf";
	  
	public static Typeface typeface1 = null;
	public static Typeface typeface2 = null;
	
	public static void SetDefaultFont(TextView v) {
	    v.setTypeface(typeface1);
	}

	public static void SetDefaultFont2(TextView v) {
	    v.setTypeface(typeface2);
	}
	
	public static boolean isNetworkStat( Context context ) {
        ConnectivityManager manager = 
           (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobile = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo lte_4g = manager.getNetworkInfo(ConnectivityManager.TYPE_WIMAX);
        boolean blte_4g = false;
        if(lte_4g != null)                             
            blte_4g = lte_4g.isConnected();
        if( mobile != null ) {
            if (mobile.isConnected() || wifi.isConnected() || blte_4g)
                  return true;
        } else {
            if ((wifi != null && wifi.isConnected()) || blte_4g )
                  return true;
        }
         
        Log.d("isNetworkStat", "Network Error");
        return false; 
	}
	
	public static int deviceWidth = 0;
	public static int deviceHeight = 0;
	public static int dipWidth = 0;
	public static int dipHeight = 0;
	
	
	
	
	private void startDownload_EdumaxAudio() {
	    String url0 = "http://studyview.co.kr/ms4/bf.MS4";
	    String url1 = "http://studyview.co.kr/ms4/bn.MS4";
	    new AsyncTask<String, String, String>(){
	    	
	    	@Override
	    	protected void onPreExecute() { 
	    		/*
	    	    mDlg = new ProgressDialog(VideoAct.this);
	    	    mDlg.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
	    	    mDlg.setMessage("Initializing EDUMAX...");
	    	    mDlg.show();
	    	 */
	    	    super.onPreExecute();
	    	}

			@Override
			protected String doInBackground(String... params) {
				// TODO Auto-generated method stub
				int count = 0;
			     
			    try {
			    	
			    	for(int i=0; i<2; i++)
			    	{
				        Thread.sleep(10);
				        URL url = new URL(params[0].toString());
				        URLConnection conexion = url.openConnection();
				        conexion.connect();
				 
				        int lenghtOfFile = conexion.getContentLength();
				        Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
				 
				        InputStream input = new BufferedInputStream(url.openStream());
				        OutputStream output = new FileOutputStream(Global.ex_storage + "/visangedu/" + (i == 0 ? "bf.MS4" : "bn.MS4"));
				 
				        byte data[] = new byte[1024];
				 
				        long total = 0;
				 
				        while ((count = input.read(data)) != -1) {
				            total += count;
				            publishProgress("" + (int) ((total * 100) / lenghtOfFile));
				            output.write(data, 0, count);
				        }
				 
				        output.flush();
				        output.close();
				        input.close();
			    	}
			         
			    } catch (InterruptedException e) {
			        e.printStackTrace();
			    } catch (IOException e) {
			        e.printStackTrace();
			    }
			 
			    // 수행이 끝나고 리턴하는 값은 다음에 수행될 onProgressUpdate 의 파라미터가 된다
			    return null;
			}
			
			@Override
			protected void onProgressUpdate(String... progress) {
			}
			
			@SuppressWarnings("deprecation")
			@Override
			protected void onPostExecute(String unused) {
							     
			}
	    	
	    }.execute(url0, url1, "1", "1");
	} 
}


